package com.codedancer.campus.manager.schedule;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.codedancer.campus.enums.SystemMessageType;
import com.codedancer.campus.manager.mq.producer.MqSendMessageProducer;
import com.codedancer.campus.manager.service.model.SystemMessageMqParamModel;
import com.codedancer.campus.mapper.*;
import com.codedancer.campus.po.Commodity;
import com.codedancer.campus.po.ConfessionalWall;
import com.codedancer.campus.po.Dynamic;
import com.codedancer.campus.po.LostFoundItem;
import com.codedancer.campus.qo.commodity.CommodityPageSearchQo;
import com.codedancer.campus.qo.confessionalWall.ConfessionalWallSearchQo;
import com.codedancer.campus.qo.dynamic.DynamicSearchQo;
import com.codedancer.campus.qo.lostFoundItem.LostFoundItemPageSearchQo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

import static com.codedancer.campus.enums.CommodityStatus.NOT_PASS;

/**
 * 删除未通过的数据信息
 * 目前主要是闲置商品、表白墙、失物招领、动态
 */
@Component
@Slf4j
public class DataDeleteSchedule {

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private CommodityCollectionMapper commodityCollectionMapper;
    @Autowired
    private CommodityImageMapper commodityImageMapper;
    @Autowired
    private CommodityExamineRecordMapper commodityExamineRecordMapper;
    @Autowired
    private LostFoundItemMapper lostFoundItemMapper;
    @Autowired
    private LostFoundItemImageMapper lostFoundItemImageMapper;
    @Autowired
    private LostFoundItemExamineRecordMapper lostFoundItemExamineRecordMapper;
    @Autowired
    private LostFoundItemCommentMapper lostFoundItemCommentMapper;
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private DynamicCommentMapper dynamicCommentMapper;
    @Autowired
    private DynamicImageMapper dynamicImageMapper;
    @Autowired
    private DynamicExamineRecordMapper dynamicExamineRecordMapper;
    @Autowired
    private DynamicKudosMapper dynamicKudosMapper;
    @Autowired
    private ConfessionalWallMapper confessionalWallMapper;
    @Autowired
    private ConfessionalWallKudosMapper confessionalWallKudosMapper;
    @Autowired
    private ConfessionalWallImageMapper confessionalWallImageMapper;
    @Autowired
    private ConfessionalWallExamineRecordMapper confessionalWallExamineRecordMapper;
    @Autowired
    private ConfessionalWallCommentMapper confessionalWallCommentMapper;
    @Autowired
    private MqSendMessageProducer mqSendMessageProducer;

    /**
     * 每天凌晨1点执行一次
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void deleteNoPassData() {

        // 商品删除
        deleteCommdity();
        // 表白墙删除
        deleteConfessionalWall();
        // 失物招领删除
        deleteLostFoundItem();
        // 树洞动态删除
        deleteDynamic();

    }

    private void deleteDynamic() {
        DynamicSearchQo dynamicSearchQo = new DynamicSearchQo();
        dynamicSearchQo.setStatus(NOT_PASS.getValue());
        // 查询出审核状态为未通过的数据
        List<Dynamic> dynamicList = dynamicMapper.listExportData(dynamicSearchQo);

        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        Date now = DateUtil.date();

        for (Dynamic dynamic : dynamicList) {
            Date updateTime = dynamic.getUpdateTime();
            long days = DateUtil.between(updateTime, now, DateUnit.DAY);
            if (days >= 3 && days < 5) {
                // 1，未通过并且最后一次修改时间大于3天小于5天，只需要通知用户即将删除
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + dynamic.getContent() + "】未通过审核且已搁置超过3天，请尽快处理，否则系统将进行自动清除")
                    .receiveUserUuid(dynamic.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            } else if (days >= 5) {
                // 2. 未通过并且最后一次修改时间大于5天，需要通知用户已被删除，并且删除掉想相关数据
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + dynamic.getContent() + "】已搁置超过5天，系统已将其清除")
                    .receiveUserUuid(dynamic.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
                // 删除数据
                dynamicMapper.deleteById(dynamic.getUuid());
                dynamicCommentMapper.deleteByColumn("dynamic_uuid", dynamic.getUuid());
                dynamicImageMapper.deleteByColumn("dynamic_uuid", dynamic.getUuid());
                dynamicExamineRecordMapper.deleteByColumn("dynamic_uuid", dynamic.getUuid());
                dynamicKudosMapper.deleteByColumn("dynamic_uuid", dynamic.getUuid());
            }
        }
    }

    private void deleteConfessionalWall() {
        ConfessionalWallSearchQo confessionalWallSearchQo = new ConfessionalWallSearchQo();
        confessionalWallSearchQo.setStatus(NOT_PASS.getValue());
        // 查询出审核状态为未通过的数据
        List<ConfessionalWall> confessionalWallList = confessionalWallMapper.listExportConfessionalWall(confessionalWallSearchQo);

        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        Date now = DateUtil.date();

        for (ConfessionalWall confessionalWall : confessionalWallList) {
            Date updateTime = confessionalWall.getUpdateTime();
            long days = DateUtil.between(updateTime, now, DateUnit.DAY);
            if (days >= 3 && days < 5) {
                // 1，未通过并且最后一次修改时间大于3天小于5天，只需要通知用户即将删除
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + confessionalWall.getContent() + "】未通过审核且已搁置超过3天，请尽快处理，否则系统将进行自动清除")
                    .receiveUserUuid(confessionalWall.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            } else if (days >= 5) {
                // 2. 未通过并且最后一次修改时间大于5天，需要通知用户已被删除，并且删除掉想相关数据
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + confessionalWall.getContent() + "】已搁置超过5天，系统已将其清除")
                    .receiveUserUuid(confessionalWall.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
                // 删除数据
                confessionalWallMapper.deleteById(confessionalWall.getUuid());
                confessionalWallCommentMapper.deleteByColumn("confessional_wall_uuid", confessionalWall.getUuid());
                confessionalWallImageMapper.deleteByColumn("confessional_wall_uuid", confessionalWall.getUuid());
                confessionalWallExamineRecordMapper.deleteByColumn("confessional_wall_uuid", confessionalWall.getUuid());
                confessionalWallKudosMapper.deleteByColumn("confessional_wall_uuid", confessionalWall.getUuid());
            }
        }
    }

    private void deleteLostFoundItem() {
        LostFoundItemPageSearchQo lostFoundItemPageSearchQo = new LostFoundItemPageSearchQo();
        lostFoundItemPageSearchQo.setStatus(NOT_PASS.getValue());
        // 查询出审核状态为未通过的数据
        List<LostFoundItem> lostFoundItemList = lostFoundItemMapper.listExportLostFoundItem(lostFoundItemPageSearchQo);

        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        Date now = DateUtil.date();

        for (LostFoundItem lostFoundItem : lostFoundItemList) {
            Date updateTime = lostFoundItem.getUpdateTime();
            long days = DateUtil.between(updateTime, now, DateUnit.DAY);
            if (days >= 3 && days < 5) {
                // 1，未通过并且最后一次修改时间大于3天小于5天，只需要通知用户即将删除
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + lostFoundItem.getName() + "】未通过审核且已搁置超过3天，请尽快处理，否则系统将进行自动清除")
                    .receiveUserUuid(lostFoundItem.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            } else if (days >= 5) {
                // 2. 未通过并且最后一次修改时间大于5天，需要通知用户已被删除，并且删除掉想相关数据
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + lostFoundItem.getName() + "】已搁置超过5天，系统已将其清除")
                    .receiveUserUuid(lostFoundItem.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
                // 删除数据
                lostFoundItemMapper.deleteById(lostFoundItem.getUuid());
                lostFoundItemCommentMapper.deleteByColumn("lost_found_item_uuid", lostFoundItem.getUuid());
                lostFoundItemImageMapper.deleteByColumn("lost_found_item_uuid", lostFoundItem.getUuid());
                lostFoundItemExamineRecordMapper.deleteByColumn("lost_found_item_uuid", lostFoundItem.getUuid());
            }
        }
    }

    private void deleteCommdity() {
        CommodityPageSearchQo commodityPageSearchQo = new CommodityPageSearchQo();
        commodityPageSearchQo.setStatus(NOT_PASS.getValue());
        // 查询出审核状态为未通过的数据
        List<Commodity> commodityList = commodityMapper.getCommodityList(commodityPageSearchQo);

        //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
        Date now = DateUtil.date();

        for (Commodity commodity : commodityList) {
            Date updateTime = commodity.getUpdateTime();
            long days = DateUtil.between(updateTime, now, DateUnit.DAY);
            if (days >= 3 && days < 5) {
                // 1，未通过并且最后一次修改时间大于3天小于5天，只需要通知用户即将删除
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + commodity.getTitle() + "】未通过审核且已搁置超过3天，请尽快处理，否则系统将进行自动清除")
                    .receiveUserUuid(commodity.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            } else if (days >= 5) {
                // 2. 未通过并且最后一次修改时间大于5天，需要通知用户已被删除，并且删除掉想相关数据
                SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                    .messageType(SystemMessageType.MESSAGE_EMPTY)
                    .content("【" + commodity.getTitle() + "】已搁置超过5天，系统已将其清除")
                    .receiveUserUuid(commodity.getPublishUserUuid())
                    .build();
                mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
                // 删除数据
                commodityMapper.deleteById(commodity.getUuid());
                commodityCollectionMapper.deleteByColumn("commodity_uuid", commodity.getUuid());
                commodityImageMapper.deleteByColumn("commodity_uuid", commodity.getUuid());
                commodityExamineRecordMapper.deleteByColumn("commodity_uuid", commodity.getUuid());
            }
        }
    }
}
